package cn.tedu.mall.order.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.domain.CsmallAuthenticationInfo;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.order.mapper.OmsCartMapper;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.model.OmsCart;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OmsCartServiceImpl implements IOmsCartService {
    @Autowired
    private OmsCartMapper  cartMapper;

    //新增商品到购物车
    @Override
    public void addCart(CartAddDTO cartDTO) {
        //获取当前登录用户的id
        Long userId = getUserId();
        //获取要添加的商品的skuId
        Long skuId = cartDTO.getSkuId();
        //根据userId和skuId去判断购物车表中是否有该商品
        OmsCart omsCart = cartMapper.selectSkuExistsCart(userId, skuId);
        //判断omsCart是否为null
        if(omsCart==null){
            //没有商品，新增商品到购物车
            //新增商品时需要的是OmsCart对象，需要将CartAddDTO转成OmsCart
            OmsCart newOmsCart = new OmsCart();
            BeanUtils.copyProperties(cartDTO,newOmsCart);
            //cartAddDTO中没有userId，需要单独对该属性进行赋值
            newOmsCart.setUserId(userId);
            cartMapper.saveCart(newOmsCart);
        }else{
            //有，修改数量
            //需要将购物车中数量和cartDTO中的数量，加在一起
           Integer quantity = omsCart.getQuantity()+ cartDTO.getQuantity();
           //赋值到omsCart中，进行数量的修改
            omsCart.setQuantity(quantity);
            cartMapper.updateQuantityById(omsCart);
        }
    }
    //根据用户id查询购物车sku列表
    @Override
    public JsonPage<CartStandardVO> listCarts(Integer page, Integer pageSize) {
        //设置分页条件
        PageHelper.startPage(page,pageSize);
        //获取用户id
        Long userId = getUserId();
        //执行查询
        List<CartStandardVO> list = cartMapper.selectCartsByUserId(userId);
        return JsonPage.restPage(new PageInfo<>(list));
    }
    //选中多个商品进行删除
    @Override
    public void removeCart(Long[] ids) {
        int rows = cartMapper.deleteCartsByIds(ids);
        if (rows==0)
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"您要删除的商品不存在");
    }

    @Override
    public void removeAllCarts() {
        Long userId = getUserId();
        int rows = cartMapper.deleteCartsByUserId(userId); if (rows==0)
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"您的购物车中没有商品");
    }

    @Override
    public void removeUserCarts(OmsCart omsCart) {
        cartMapper.deleteCartByUserIdAndSkuId(omsCart);
    }

    @Override
    public void updateQuantity(CartUpdateDTO cartUpdateDTO) {
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(cartUpdateDTO,omsCart);
        cartMapper.updateQuantityById(omsCart);
    }

    //我们单独写一个方法获取用户信息
    public CsmallAuthenticationInfo getUserInfo(){
        //获取SpringSecurity上下文，获取token
        UsernamePasswordAuthenticationToken token =
                (UsernamePasswordAuthenticationToken)
                        SecurityContextHolder.getContext().getAuthentication();
        //判断token是否为null
        if(token==null)
            throw new CoolSharkServiceException(ResponseCode.UNAUTHORIZED,"没有登录信息");
        //获取用户信息
        CsmallAuthenticationInfo userInfo =
                (CsmallAuthenticationInfo) token.getCredentials();
        //返回登录用户信息
        return userInfo;
    }
    //单独写一个获取userId的方法
    public Long getUserId(){
        return getUserInfo().getId();
    }

}
